<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <code-tech.diandian.com>
// +----------------------------------------------------------------------
namespace Home\Model;
use Think\Model;

/**
 * 插件模型
 * @author yangweijie <yangweijiester@gmail.com>
 */

class ArticleshowModel extends Model {

    protected $_validate = array(    
    );

    /* 自动完成规则 */
    protected $_auto = array(
       
    );
    //$id(-1:分页获取文章,0:获取全部文章,大于0:获取对应ID文章)
    //$type(1:热销,2:推荐)
    //$status(-1:删除,0:禁用,1:正常,2:待审核,3:草稿)
    public function articleShowGet($id = -1,$type = 0,$page = 1,$limit = 10,$order = 'create_time',$status = 1,$filter,$search){
    	//获取当前文章的状态
    	
        $aswhere['status'] = $status;
        if($filter){
            $aswhere[$filter] = 1;
        }
        if($search){
             $aswhere[] = "title like '%$search%'";
        }
 
    	//获取文章的归属
    	if($type){
    		$aswhere['type'] = $type;
    	}
    	//获取文章的ID
    	if($id > 0){
    		//查找某一篇文章
    		$aswhere['id'] = $id;
    		$articleshowdata = $this->getArticleShowById($aswhere,$order);
           
    	}else{
    		if($id < 0){
    		//分页获取文章
    			$articleshowdata = $this->getArticleShow($aswhere,$order,$page,$limit);
    		}else{
    		//获取所有文章
    			$articleshowdata = $this->getArticleShowAll($aswhere,$order);
    		}
    	}

  		return $articleshowdata;
    }
    /*
    
    $banners = array();
            $banners =array(
                '0' => $this->imgIdToPicture($vaule['banner1']), 
                '1' => $this->imgIdToPicture($vaule['banner2']), 
                '2' => $this->imgIdToPicture($vaule['banner3']), 
                '3' => $this->imgIdToPicture($vaule['banner4']), 
                '4' => $this->imgIdToPicture($vaule['banner5']), 
                );
            $save_banners_data =array();
            foreach ($banners as $key2 => $value2) {
                if($value2){
                    $save_banners_data[] = $value2;  
                }   
            }
            if($save_banners_data){
                $save_data[$key]['banners'] = $save_banners_data;
            }else{
                $save_data[$key]['banners'] = null;
            }
     */
    public function articleCollectNumAdd($article_id){
    	$aswhere['id'] = $article_id;
    	$collect_num = $this->where($aswhere)->getField('collect_num');
    	$collect_num++;
    	$data['collect_num'] = $collect_num;
    	$this->where($aswhere)->data($data)->save();
    }

    public function getPrice($article_id,$num){
        $where['id'] = $article_id;
        switch ($num) {
            case '1':
                $column = 'one_price';
                break;
            case '2':
                $column = 'two_price';
                break;
            case '3':
                $column = 'three_price';
                break;
            default:
                $column = 'four_price';
                break;
        }
        $price = $this
        ->where($where)
        ->getField($column);
        return $price;
    }
    public function articleShowDetialByOrderIdGet($order_id){
        $articleshow_data = $this
        ->alias('arts')
        ->join('RIGHT JOIN `order` o ON arts.id = o.article_id')
        ->where('o.id ='.$order_id)
        ->find();
        return $articleshow_data;
    }

    protected function getArticleShow($aswhere,$order,$page,$limit){
    	$result = $this
    	->where($aswhere)
    	->order($order)
    	->page($page,$limit)
    	->select();
    	return $result;
    }
    protected function getArticleShowAll($aswhere,$order){
    	$result = $this
    	->where($aswhere)
    	->order($order)
    	->select();
    	return $result;
    }
    protected function getArticleShowById($aswhere,$order){
    	$result = $this
    	->where($aswhere)
    	->order($order)
    	->select();
        foreach ($result as $key => $value) {
            $banners = array();
            $artic =A('Articleshow');
            // $banners =array(
            //     '0' => $artic->imgIdToPicture($vaule['banner1']), 
            //     '1' => $artic->imgIdToPicture($vaule['banner2']), 
            //     '2' => $artic->imgIdToPicture($vaule['banner3']), 
            //     '3' => $artic->imgIdToPicture($vaule['banner4']), 
            //     '4' => $artic->imgIdToPicture($vaule['banner5']), 
            //     );
            $save_banners_data =array();
            foreach ($banners as $key2 => $value2) {
                if($value2){
                    $save_banners_data[] = $value2;  
                }   
            }
            if($save_banners_data){
                $save_data[$key]['banners'] = $save_banners_data;
            }else{
                $save_data[$key]['banners'] = null;
            }
            $result[$key]['banners'] =$banners;
        }

    	return $result;
    }
}